<?php
namespace app\api\controller\wanlshop;

use app\common\controller\Api;

/**
 * WanlShop文章接口shitu
 */
class Article extends Api
{
    protected $noNeedLogin = ['getList','details','adDetails'];
    protected $noNeedRight = ['*'];
    
	
    /**
     * 获取指定文章列表
     *
     * @ApiSummary  (WanlShop 获取文章列表)
     * @ApiMethod   (POST)
	 * 
	 * @param string $type 文章类型
	 * @param string $list_rows 每页数量
	 */
    public function getList()
    {
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		if ($this->request->isPost()) {
			$type = $this->request->post('type');
			$where['status'] = 'normal';
			$config = get_addon_config('wanlshop');
			if($type == 'help'){
				$where['category_id'] = $config['config']['help_category'];
			}
			if($type == 'new'){
				$where['category_id'] = $config['config']['new_category'];
			}
			if($type == 'sys'){
				$where['category_id'] = $config['config']['sys_category'];
			}
			$data = model('app\api\model\wanlshop\Article')
				->where($where)
				->field('id,title,description,image,images,flag,views,createtime')
				->order('createtime desc')
				->paginate();
			
			$this->success('返回成功', $data);
		}
		$this->error(__('非法请求'));
        
    }
    
	
    /**
     * 获取内容详情
     *
     * @ApiSummary  (WanlShop 获取内容详情)
     * @ApiMethod   (POST)
     * 
	 * @param string $id 文章ID
     */
    public function details()
    {
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		$id = $this->request->get('id');
		$id ? $id : ($this->error(__('Invalid parameters')));
		$row = model('app\api\model\wanlshop\Article')
			->where(['id' => $id])
			->find();
		// 1.0.5升级
		if(!$row){
			$this->error(__('没有找到任何内容'));
		}
		// 点击 +1
		$row->setInc('views');
		$this->success('返回成功', $row);
    }

    /**
     * 点赞或取消点赞
     *
     * @ApiSummary  (WanlShop 文章点赞或取消点赞)
     * @ApiMethod   (POST)
     *
     * @param string $id 文章ID
     */
    public function setLike()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            if (!$id) {
                $this->error(__('未传入正确的文章ID'));
            }
            $model = model('app\api\model\wanlshop\ArticleLike');
            $where['article_id'] = $id;
            $where['user_id'] = $this->auth->id;
            if ($model->where($where)->count() === 0) {
                $model->save($where);
                $like = model('app\api\model\wanlshop\Article')->get($id);
                $like->setInc('likecount');
                $this->success('返回成功', ['data' => 1]);
            } else {
                $model->where($where)->delete();
                $like = model('app\api\model\wanlshop\Article')->get($id);
                $like->setDec('likecount');
                $this->success('返回成功', ['data' => 0]);
            }
        }
        $this->error(__('非正常访问'));
    }
    
	
	/**
	 * 获取广告详情
	 *
	 * @ApiSummary  (WanlShop 获取内容详情)
	 * @ApiMethod   (POST)
	 * 
	 * @param string $id 文章ID
	 */
	public function adDetails($id = null)
	{
		$row = model('app\api\model\wanlshop\Advert')->get($id);
		// 1.0.5升级
		if(!$row){
			$this->error(__('没有找到任何内容'));
		}
		// 点击 +1
		$row->setInc('views');
		$this->success('返回成功', $row);
	}

	/**
	 * 商家友圈
	 *
	 * @ApiSummary  ()
	 * @ApiMethod   (POST)
	 */
	public function frends()
	{
		if ($this->request->isPost()) {
			$params = $this->request->post();
			$data = [
				'title' => $params['title'],
				'category_id' => 999,
				'images' => $params['images']
			];
			model('app\api\model\wanlshop\Article')->data($data)->save();
			$this->success('返回成功', $params);
		}
		$this->error(__('非法请求'));
	}

	/**
	 * 友圈主页
	 *
	 */
	public function frendhome()
	{
		if ($this->request->isPost()) {
			$data = model('app\api\model\wanlshop\Article')
                ->where(['category_id' => 999])
				->field('id,title,images,createtime')
				->order('id desc')
				->select();
			$this->success('返回成功', $data);
		}
		$this->error(__('非法请求'));
	}
	
}
